package springinpractice.ch2.controller;

import java.math.BigDecimal;
import java.util.Date;

import javax.naming.NamingException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.jndi.JndiTemplate;

import springinpractice.ch2.model.Department;
import springinpractice.ch2.model.Email;
import springinpractice.ch2.model.Employee;

/*
 * @author	Zeeshan Maqbool
 * Book		'Spring In Practice' [Book written by Willie Wheeler and John Wheeler]
 */

public class Main {

	final static Logger log = Logger.getLogger(Main.class);
	static ApplicationContext ctx = null;
	private static SessionFactory sessionFactory;
	private static Session session;
	
	static Department infotech;
	static Employee john;
	static Email gmailAcct ;
	
	public static void main(String[] args) throws Exception 
	{
		ctx = setContext();
		SetSessionFactory();
		
		DepartmentCreation();
		EmployeeCreation();
		EmailCreation();
		
		CloseUp();
	}

	private static ApplicationContext setContext()
	{
		/*
		 * Directory for context.xml
		 */
		String currentDir = System.getProperty("user.dir");
		String separator = System.getProperty("file.separator");
		String sourceDir = "resources" + separator;
		return new FileSystemXmlApplicationContext(separator + currentDir
				+ separator + sourceDir + "context.xml");
		
		/*
		 * Other ways to load the context file
		 */
		// Resource res1 =
		// ctx.getResource("file:///COMPLETE/PATH/TO/YOUR/FILE");
		// Resource res2 = ctx.getResource("classpath:com/yourName.class");

	}

	private static void SetSessionFactory()
	{
		sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
		//session = sessionFactory.getCurrentSession();
		session = sessionFactory.openSession();
	}
	
	private static void DepartmentCreation()
	{
		infotech = new Department("I Technology");	
		session.save(infotech);
	}
	
	private static void EmployeeCreation()
	{
		john = new Employee("John Wheeler", "Software Engineer",new BigDecimal("25000"), new Date());
		john.setDepartment(infotech);
		session.save(john);
	}
	
	private static void EmailCreation()
	{
		gmailAcct = new Email("xyz@gmail.com", "home");
		gmailAcct.setEmployee(john);
		session.save(gmailAcct);
	}
	
	private static void CloseUp()
	{
		session.flush();
		session.close();
	}


	private static void DBSetupTest() {
		/*
		 * Following will test your DB setup
		 */
		BasicDataSource dataSource = (BasicDataSource) ctx
				.getBean("dataSource");
		log.info(dataSource.getUsername());
	}

	private static void JndiSetupTest() {
		/*
		 * Following will test your JNDI setup
		 * 
		 * There are some issues with it but we are not using it so we ignore them.
		 * If you want to use JNDI for your spring application, plz search internet 
		 *
		 */
		BasicDataSource dataSource = (BasicDataSource) ctx.getBean("dataSource-jndi");
		log.info(dataSource.getUsername());
				
		//JndiObjectFactoryBean jof = (JndiObjectFactoryBean) ctx.getBean("dataSource-jndi");
		//(BasicDataSource) new JndiTemplate().lookup("java:comp/env/jdbc/SpringInPracticeDS");				
		
	}
}
